<script type="text/javascript">
var pageNumber=1,pageSize=5;
var personId="";
var $table = $('#sample_1'),
selections = [];
$(function(){
	initDept();
	initTable();
	$('#getSongPM').val($('#sing_personnel1').val());
});


function initTable(){
	var columns = [
	{
	    field: 'state',
	    checkbox: true,
	    align: 'center',
	    valign: 'middle'
    },
	{
        field: '',
        title: '序号',
        switchable:false,
    	align:"center", 
    	valign:"middle",
        formatter: function (value, row, index) {
                return index+1;
        }
    },
    {
        field: 'personnel_id',
        v_field : 'personnel_id',
        title: 'id',
        switchable:false,
    	align:"center", 
    	valign:"middle",
       	visible:false
    },
    {
    	 field : 'personnel_name',
         v_field : 'personnel_name',
         title : '员工姓名/工号',
         align : 'center',
         valign : 'middle',
         sortable : true,
         formatter: function (value, row, index) {
             return row.personnel_name + row.personnel_shortCode;
		 }
    },
    {
        field : 'dept_name',
        v_field : 'dept_name',
        title : '部门名称 ',
        align : 'center',
        valign : 'middle'
    },
    {
        field : 'personnel_postName',
        v_field : 'personnel_postName',
        title : '职务名称 ',
        align : 'center',
        valign : 'middle'
    }
    ];
    $('#sample_1').bootstrapTable({
        method:'POST',
        dataType:'json',
        contentType: "application/x-www-form-urlencoded",
        url:global_param.context_name+"/person/search.do",
        cache: false,
        striped: true,                              //是否显示行间隔色
        sidePagination: "server",           	//分页方式：client客户端分页，server服务端分页（*）
        pagination:true,
        minimumCountColumns:2,
        pageNumber:pageNumber,                       //初始化加载第一页，默认第一页
        pageSize: pageSize,    //每页的记录行数（*）
        sortName:'personnel_id',
        sortOrder:'asc',
        pageList: [5 ,10, 25, 50, 100],        //可供选择的每页的行数（*）
        uniqueId: "personnel_id",              //每一行的唯一标识，一般为主键列
        showExport: true,                    
        exportDataType: 'all',
        queryParamsType:"",
        showRefresh:false,					//是否显示刷新按钮
        search:false,						//是否显示搜索框
        showToggle:false,
        ajaxOptions: {traditional: true},
        clickToSelect: true,
		responseHandler:responseHandler, //在渲染页面数据之前执行的方法，此配置很重要!!!!!!!
        onLoadSuccess: function() {
			initTabTable($("#sample_1"));
        },
        onColumnSwitch:function(){
        	initTabTable($("#sample_1"));
        },
        onCheck: function (row) {
        	var songPM = row.personnel_name;
        	var songPMID = row.personnel_id;
        	var old = $('#getSongPM').val();
        	var oldId = $('#sing_personnel_id').val();
        	var newPm = '';
        	var newPmId = '';
        	if(old == '' || old == null){
        		newPm = songPM;
        		newPmId = songPMID;
        	}else{
        		newPm = old +'&'+ songPM;
        		newPmId = oldId +','+ songPMID;
        	}
        	$('#getSongPM').val(newPm);
    		$('#sing_personnel_id').val(newPmId);
        },
        onUncheck: function (row) {
        	var songPM = row.personnel_name;
        	var songPMID = row.personnel_id;
        	var old = $('#getSongPM').val();
        	var oldId = $('#sing_personnel_id').val();
        	var newPm = '';
        	var newPmId = '';
        	
        	var arr = old.split("&");
    		var arrId = oldId.split(",");
    		var temp = $.inArray(songPM, arr); 
    		if(temp < 0){
    			newPm = old + "&" + songPM;
    			newPmId = oldId + "," + songPMID;
    		}else{
    			arr.splice($.inArray(songPM,arr),1);
    			for(x in arr){
    				newPm += arr[x]+"&";
    			}
    			newPm = newPm.substring(0,newPm.length-1);
    			
    			arrId.splice($.inArray(songPMID+'',arrId),1);
    			for(x in arrId){
    				newPmId += arrId[x]+",";
    			}
    			newPmId = newPmId.substring(0,newPmId.length-1);
    		}
    		$('#getSongPM').val(newPm);
    		$('#sing_personnel_id').val(newPmId);
        },
        onCheckAll: function (rows) {
        	var newPm = '';
        	var newPmId = '';
        	for(var i=0;i<rows.length;i++){
        		newPm += rows[i].personnel_name+'&';
        		newPmId += rows[i].personnel_id+',';
        	}
        	newPm = newPm.substring(0,newPm.length-1);
        	newPmId = newPmId.substring(0,newPmId.length-1);
        	
        	var old = $('#getSongPM').val();
        	var oldId = $('#sing_personnel_id').val();
        	if(old != '' && old != null){
        		newPm = old +"&"+newPm;
        		newPmId = oldId +","+newPmId;
        	}
        	$('#getSongPM').val(newPm);
    		$('#sing_personnel_id').val(newPmId);
        },
        onUncheckAll: function (rows) {
        	var old = $('#getSongPM').val();
        	var oldId = $('#sing_personnel_id').val();
        	
        	var arr = old.split("&");
    		var arrId = oldId.split(",");
    		for(var i=0;i<rows.length;i++){
    			var newPm = '';
            	var newPmId = '';
       			arr.splice($.inArray(rows[i].personnel_name,arr),1);
       			for(x in arr){
       				newPm += arr[x]+"&";
       			}
       			newPm = newPm.substring(0,newPm.length-1);
       			
       			arrId.splice($.inArray(rows[i].personnel_id+'',arrId),1);
       			for(x in arrId){
       				newPmId += arrId[x]+",";
       			}
       			newPmId = newPmId.substring(0,newPmId.length-1);
    		}
    		
    		$('#getSongPM').val(newPm);
    		$('#sing_personnel_id').val(newPmId);
        },
        queryParams: function queryParams(params) { //设置查询参数  
          var param = {    
        	  page: params.pageNumber,    
              pagesize: params.pageSize,
              sortname :params.sortName,
              sortorder :params.sortOrder,
              name:$("#name").val(),
              dept_name:$("#dept_name").zxCombotree('getValue'),              
          };
          return param;                   
        },  
        columns: columns
    });
}

//表格分页之前处理多选框数据
function responseHandler(res) {
	var oldId = $('#sing_personnel_id').val();
	var arr = oldId.split(",");
    $.each(res.rows, function (i, row) {
        row.state = $.inArray(row.personnel_id+"", arr) != -1;	//判断当前行的数据id是否存在与选中的数组，存在则将多选框状态变为true
    });
    return res;
}	

function initDept(){
	$("#dept_name").zxCombotree({
        data:''
    });
	
	$("#dept_name").val("--请选择--");
	//部门名称下拉列表初始化 
    $.ajax({
         type: "POST",
         url: global_param.context_name + "/depart/getParentDeptByOrgId.do",
         async: false,
         success: function(result) {
             var jsonStr = eval("(" + result + ")");
             $("#dept_name").zxCombotree({
                data:jsonStr,
                //宽度
                width:184,
                //不可编辑
                editable:false
            }); 
         }
     });
	
}

function searchUser(){
	$('#sample_1').bootstrapTable('refresh',{url: global_param.context_name+"/person/search.do",pageNumber:pageNumber,pageSize:pageSize});
}

function clearQueryParams(){
	$('#name').val("");	
	initDept();
}

function queryParams()
{
 	$('#sample_1').bootstrapTable('refresh',{url: global_param.context_name+"/person/search.do"});
}

function showPersonInfo(id){
	personId = id;
	BootstrapDialog.show({
        message: $('<div id="reportDataViewDiv" class="messageDet"></div>').load('html/personManage/personInfoView.html'),
        title: '用户信息',
        titleClass: 'text-center',
        closable: false,
        size:BootstrapDialog.SIZE_WIDE,
        buttons: [{
            label: '关闭',
            cssClass:'btn btn-default col-sm-4 col-sm-offset-4 col-xs-6 col-xs-offset-3',
            action: function(dialog) {
            	$("#reportDataViewDiv").html("");
                dialog.close();
            }
        }]
   });
}

function evaluateSongPM(){
	var Pm = $('#getSongPM').val();
	$('#sing_personnel1').val(Pm);
}

</script>

<section class="content item">
	 <div class="container-fluid">
		<div class="row">
			<div class="box">
        		<div class="box-body">
        			<div class="col-lg-4 col-md-2 col-sm-4 col-xs-12">
		            	<div class="form-group">
		             	  <label for="dept_name">部门名称</label>
		             	  <input type="text" class="form-control" id="dept_name"/>
		                </div>
		            </div>
		            <div class="col-lg-4 col-md-2 col-sm-2 col-xs-12">
		            	<div class="form-group">
			                <label for="name">员工姓名/工号</label>
							<input type="text" class="form-control"  id="name" style="width: 100%;" placeholder="请输入姓名/工号" >
		                </div>
		            </div>
		            <div class="col-lg-4 col-md-3 col-sm-6 col-xs-12">
		            	<div class="form-group">
		             	   <label>&nbsp;</label>
		             	   <div class="row">
							   <div class="col-xs-6">
								   <button type="button" class="btn btn-block btn-primary" onclick="searchUser()">查询</button>
							   </div>
							   <div class="col-xs-6">
								   <button type="button" class="btn btn-block btn-default" onclick="clearQueryParams()">清空</button>
							   </div>
						   </div>
					    </div>
		            </div>
		    	</div>
		   </div>
		</div>
    </div>
    
	<div class="row">
        <div class="col-xs-12">
            <div class="box box-primary">
              <div class="box-header">
                 <h3 class="box-title">数据列表</h3>
              </div>
              <div class="box-body">
              	<table id="sample_1">
                </table>
                <textarea id="getSongPM" readonly="readonly" style="width: 880px; height: 70px;"></textarea>
			  </div>
            </div>
         </div>
     </div>
</section>